package com.mark.controller.portal;

import com.mark.common.ServerResponse;
import com.mark.controller.common.CurrentUser;
import com.mark.enums.UserStateEnum;
import com.mark.pojo.Seller;
import com.mark.pojo.User;
import com.mark.service.ISellerService;
import com.mark.util.CookieUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by Choisaaaa on 2018/5/22.
 * 卖家信息管理控制器
 */
@Controller
@RequestMapping("/seller/")
public class SellerController {
    @Autowired
    private ISellerService iSellerService;

    //1.用户注册
    @RequestMapping(value = "register.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> register(Seller seller, HttpSession session, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)  {
        String loginToken = CookieUtil.readLoginToken(httpServletRequest);
        if(seller == null ){
            return ServerResponse.createByIllegalArgument("店家注册信息不能为空");
        }
        ServerResponse response = CurrentUser.getCurrentUser(httpServletRequest);
        if (!response.isSuccess()){
            return response;
        }
        User currentUser = (User) response.getData();
        if(currentUser.getRole() == UserStateEnum.SELLER_CUSTOMER.getRole()){
            return ServerResponse.createByErrorMessage("你已经注册成为店家了！请勿重复操作");
        }
        seller.setUserId(currentUser.getId());
        return  iSellerService.register(seller,loginToken);
    }
}
